/*
 * note.c
 *
 *  Created on: Mar 8, 2013
 *      Author: George
 */
#include "common.h"




void set_note_duration(int dur, int tempo)
{
	
	switch(tempo)					
	{
		case 0:								//60BPM
		{
			MCF_PIT0_PMR = 9765;			//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8;
			break;
		}
		case 1:								//70BPM
		{
			MCF_PIT0_PMR = 8370;
			//MCF_PIT0_PMR = 11394;			//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL
			break;
		}
		case 2:								//80BPM
		{
			MCF_PIT0_PMR = 7324;
			//MCF_PIT0_PMR = 13020;			//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL		
			break;
		}
		case 3:								//90BPM
		{
			MCF_PIT0_PMR = 6510;
			//MCF_PIT0_PMR = 14647;		 	//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL
			break;
		}	
		case 4:								//100BPM
		{
			MCF_PIT0_PMR = 5859;
			//MCF_PIT0_PMR = 16275;		 	//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL		
			break;
		}
		case 5:								//110BPM
		{
			MCF_PIT0_PMR = 5326;
			//MCF_PIT0_PMR = 17902;			//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL
			break;
			
		}
		case 6:								//120BPM
		{
			MCF_PIT0_PMR = 4882;		 	//MCF_PIT0_PMR = 4883; // set PMR with the desired value.
			//MCF_PIT0_PMR = 19531;
			MCF_PIT0_PCSR |= (10) << 8; 		// 4 BIT PRESCALER PG 431 MANUAL
			break;
		}
		
		
	}
	

}

void set_pitch(int pitch)
{
	
	switch(pitch)
	{
	case (0x0):			//B3 = 246.94
	{
		MCF_PWM_PWMPER(7) = 250;
		MCF_PWM_PWMSCLB = 162;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
		
	case 0x01: 			//C4 = 261.63
	{
		MCF_PWM_PWMPER(7) = 233;
		MCF_PWM_PWMSCLB = 164;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x02: 			//C4# = 277.18 
	{
		MCF_PWM_PWMPER(7) = 220;
		MCF_PWM_PWMSCLB = 164;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x03: 			//D4 = 293.67
	
	{
		MCF_PWM_PWMPER(7) = 245;
		MCF_PWM_PWMSCLB = 139;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
					
	case 0x04: 			//D4# = 311.3
	{
		MCF_PWM_PWMPER(7) = 206;
		MCF_PWM_PWMSCLB = 156;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x05: 			//E4 = 329.63
	{
		MCF_PWM_PWMPER(7) = 192;
		MCF_PWM_PWMSCLB = 158;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x06: 			//F4 = 349.23
	{
		MCF_PWM_PWMPER(7) = 139;
		MCF_PWM_PWMSCLB = 206;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x07: 			//F4# = 369.99
	{
		MCF_PWM_PWMPER(7) = 118;
		MCF_PWM_PWMSCLB = 229;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x08: 			//G4 = 392.00
	{
		MCF_PWM_PWMPER(7) = 117;
		MCF_PWM_PWMSCLB = 218;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 0x09: 			//G4# = 415.3
	{
		MCF_PWM_PWMPER(7) = 121;
		MCF_PWM_PWMSCLB = 199;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 10: 			//A4 = 440.00
	{
		MCF_PWM_PWMPER(7) = 247;
		MCF_PWM_PWMSCLB = 92;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 11: 			//A4# = 466.16
	{
		MCF_PWM_PWMPER(7) = 241;
		MCF_PWM_PWMSCLB = 89;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 12: 			//B5 = 493.88
	{
		MCF_PWM_PWMPER(7) = 106;
		MCF_PWM_PWMSCLB = 191;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}
	case 13: 			//C5 = 523.25
	{
		MCF_PWM_PWMPER(7) = 245;
		MCF_PWM_PWMSCLB = 78;
		MCF_PWM_PWMPRCLK |= 1;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}	
	case 14: 			//C5# = 554.365
		{
			MCF_PWM_PWMPER(7) = 164;
			MCF_PWM_PWMSCLB = 110;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}	
	case 15: 			//D5 = 587.329
		{
			MCF_PWM_PWMPER(7) = 75;
			MCF_PWM_PWMSCLB = 227;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}	
	case 16: 			//D#5 = 622.254
		{
			MCF_PWM_PWMPER(7) = 196;
			MCF_PWM_PWMSCLB = 82;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}	
	case 17: 			//E5 = 659.255
		{
			MCF_PWM_PWMPER(7) = 158;
			MCF_PWM_PWMSCLB = 96;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}	

	case 18: 			//F5 = 698.456
			{
				MCF_PWM_PWMPER(7) = 103;
				MCF_PWM_PWMSCLB = 139;
				MCF_PWM_PWMPRCLK |= 1;
				MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
				MCF_PWM_PWME |= (1<<7);
				break;
			}
	case 19: 			//F5# = 739.98
			{
				MCF_PWM_PWMPER(7) = 85;
				MCF_PWM_PWMSCLB = 159;
				MCF_PWM_PWMPRCLK |= 1;
				MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
				MCF_PWM_PWME |= (1<<7);
				break;
			}
	case 20: 			//G5 = 783.99
			{
				MCF_PWM_PWMPER(7) = 109;
				MCF_PWM_PWMSCLB = 117;
				MCF_PWM_PWMPRCLK |= 1;
				MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
				MCF_PWM_PWME |= (1<<7);
				break;
			}
	case 21:			//Ab5 = 830.6
		{
			MCF_PWM_PWMPER(7) = 219;
			MCF_PWM_PWMSCLB = 55;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}
	
	case 22:			//A5 = 880.00
		{
			MCF_PWM_PWMPER(7) = 160;
			MCF_PWM_PWMSCLB = 71;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}
		
	case 23:
		{				//Bb5 = 932.3
			MCF_PWM_PWMPER(7) = 149;
			MCF_PWM_PWMSCLB = 72;
			MCF_PWM_PWMPRCLK |= 1;
			MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
			MCF_PWM_PWME |= (1<<7);
			break;
		}	
	default:			//We can set the frequency to some large number and that will represent a pause
	{
		MCF_PWM_PWMPER(7) = 0;
		MCF_PWM_PWMSCLB = 0;
		MCF_PWM_PWMPRCLK |= 0;
		MCF_PWM_PWMDTY(7) = MCF_PWM_PWMPER(7)*.2;
		MCF_PWM_PWME |= (1<<7);
		break;
	}	
	}
	
}

void play_note(int pitch, int dur, int tempo)
{
	set_pitch(pitch);
	set_note_duration(dur, tempo);
	blink_led(pitch);
	
}